package org.jeecg.modules.simulator.model.line.park.load;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.jeecg.modules.simulator.model.line.park.Parker;
import org.jeecg.modules.simulator.model.line.park.Parker1;
import org.jeecg.modules.simulator.model.line.park.Parker2;
import lombok.Getter;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: gp2
 * @description:
 * @author: renzhg
 * @create: 2024-08-29 14:04
 **/
public class ParkJsonFactory  implements ParkerLoader{

    public static final  String PARKING="parking";
    public static final  String YARDS="yards";
    @Getter
    Map<String, List<Parker>>  parkerMap=new HashMap<>();

    public void readFileLoad(String linepath){
//         List<Parker> parkerList=new ArrayList<>();
        try {
            String jsonString = new String(Files.readAllBytes(Paths.get(linepath)));
            JSONArray jsonArray = JSON.parseArray(jsonString);
            List<Parker> parks=new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                String typepark=jsonArray.getJSONObject(i).getString("parkType");
                if(typepark.equals(PARKING)){
                    Parker parker = new Parker1();
                    parker.setName(jsonArray.getJSONObject(i).getString("name"));
                    parker.setUpOutStationId(jsonArray.getJSONObject(i).getString("upOut"));
                    parker.setDownOutStationId(jsonArray.getJSONObject(i).getString("downOut"));
                    parker.setId(jsonArray.getJSONObject(i).getString("id"));
                    parker.setLineId(jsonArray.getJSONObject(i).getString("lineId"));
                    parker.setOffset(jsonArray.getJSONObject(i).getLong("offset"));
                    parker.setOpen(jsonArray.getJSONObject(i).getBoolean("open"));
                    parker.setParkType(typepark);
                    parks.add(parker);
                }else if(typepark.equals(YARDS)){
                    Parker parker = new Parker2();
                    parker.setName(jsonArray.getJSONObject(i).getString("name"));

                    parker.setUpOutStationId(jsonArray.getJSONObject(i).getString("upOut"));
                    parker.setDownOutStationId(jsonArray.getJSONObject(i).getString("downOut"));
                    parker.setId(jsonArray.getJSONObject(i).getString("id"));
                    parker.setLineId(jsonArray.getJSONObject(i).getString("lineId"));
                    parker.setOffset(jsonArray.getJSONObject(i).getLong("offset"));
                    parker.setOpen(jsonArray.getJSONObject(i).getBoolean("open"));
                    parker.setParkType(typepark);
                    parks.add(parker);

                }





            }

            parkerMap= parks.stream().collect(Collectors.groupingBy(o -> o.getLineId()));


//           List<Parker> parks = JSONArray.parseArray(jsonString, Parker.class);
//           parkerMap.put(lineId,parks);
        }catch (Exception e){
            e.printStackTrace();
        }

//         try {
//
//              JSONArray jsonArray = JSON.parseArray(jsonString);
//              for (int i = 0; i < jsonArray.size(); i++) {
//
//
//
//                Parker parker = JSONArray.parseObject().parseObject(jsonArray.getJSONObject(i).toString(),);
//                parkerList.add(parker);
//              }
//
//         } catch (IOException e) {
//              e.printStackTrace();
//         }


    }

    @Override
    public void loadParkData(){
        readFileLoad("D:/aaaa/parker.json");

        //TODO
    }



}
